package com.zjitc.controller;

import com.zjitc.model.*;
import com.zjitc.service.CategoryService;
import com.zjitc.service.ProductService;
import com.zjitc.utils.redis.MyRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpSession;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/5 0005
 * @description:
 */
@Controller
@RequestMapping(value = "/product")
public class ProductControl {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private MyRedis redis;

    /**
     * 通过pid查找商品
     */
    @RequestMapping(value = "/productInfo")
    public ModelAndView findByPid(String pid, HttpSession session) {
        Product product = productService.findByPid(pid);
        Category category = categoryService.findByCid(product.getCid());
        product.setCategory(category);
        ModelAndView mv = new ModelAndView(new InternalResourceView("/jsp/product_info.jsp"));
        mv.addObject("product", product);
        makeHistory(session, product);
        return mv;
    }

    /**
     * 浏览记录
     */
    private void makeHistory(HttpSession session, Product product) {
        Object o = session.getAttribute("history");
        History history = null;
        if (o == null) {
            history = new History();
            session.setAttribute("history", history);
        } else {
            history = (History) o;
        }
        history.add(product);
    }

    //    @RequestMapping(value = "/findProductByCid")
//    @ResponseBody
//    public ModelAndView findByCid(
//            @RequestParam(name = "cid", required = true) String cid,
//            @RequestParam(name = "pageId", required = false, defaultValue = "1") int pageId,
//            @RequestParam(name = "pageSize", required = false, defaultValue = "12") int pageSize) {
//        System.out.println(pageId);
//        System.out.println(pageSize);
//        List<Product> products = productService.findByCid(cid, pageId, pageSize);
//        PageInfo info = new PageInfo(products);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setView(new InternalResourceView("/jsp/product_list.jsp"));
//        modelAndView.addObject("page", info);
//        modelAndView.addObject("cid", cid);
//        return modelAndView;
//    }

    /**
     * 分类商品的缓存分页
     *
     * @param cid
     * @param pageId
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/findCatchByCidAndPid")
    @ResponseBody
    public ModelAndView findByCid2(
            @RequestParam(name = "cid", required = true) String cid,
            @RequestParam(name = "pageId", required = false, defaultValue = "0") int pageId,
            @RequestParam(name = "pageSize", required = false, defaultValue = "12") int pageSize) {
        Page<Product> page = productService.findByCatchByCid(cid, pageId, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(new InternalResourceView("/jsp/product_list.jsp"));
        modelAndView.addObject("page", page);
        modelAndView.addObject("cid", cid);
        return modelAndView;
    }


    @RequestMapping(value = "/findLike")
    @ResponseBody
    public ModelAndView findLike(
            @RequestParam(name = "name") String name,
            @RequestParam(name = "pageId", required = false, defaultValue = "0") int pageId,
            @RequestParam(name = "pageSize", required = false, defaultValue = "12") int pageSize,
            HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();

        User user = (User) session.getAttribute("user");
        if (user == null) {
            String msg = "请先登录";
            String address = "/jsp/login.jsp";
            modelAndView.setView(new InternalResourceView("/jsp/msg.jsp"));
            modelAndView.addObject("msg", msg);
            modelAndView.addObject("address", address);
            return modelAndView;
        }
        redis.sadd(user.getUid() + "search", name);
        Page<Product> page = productService.findLike(name, pageId, pageSize);
        modelAndView.setView(new InternalResourceView("/jsp/product_search.jsp"));
        modelAndView.addObject("page", page);
        modelAndView.addObject("name", name);
        return modelAndView;
    }

    @RequestMapping(value = "/findProductByFlag")
    @ResponseBody
    public ModelAndView findByFlag(
            @RequestParam(name = "flag", required = true, defaultValue = "1") int flag,
            @RequestParam(name = "pageId", required = false, defaultValue = "0") int pageId,
            @RequestParam(name = "pageSize", required = false, defaultValue = "12") int pageSize) {
        Page<Product> page = productService.findByFlag(flag, pageId, pageSize);
//        PageInfo info = new PageInfo(products);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(new InternalResourceView("/jsp/secKill.jsp"));
        modelAndView.addObject("page", page);
        modelAndView.addObject("flag", flag);
        return modelAndView;
    }

    @RequestMapping(value = "/verifyFlag")
    @ResponseBody
    public String verifyFlag(@RequestParam(name = "flag", required = false, defaultValue = "1") int flag,
                             @RequestParam(name = "pageId", required = false, defaultValue = "0") int pageId,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "12") int pageSize) {
        Page<Product> page = productService.findByFlag(flag, pageId, pageSize);
        if (page != null) {
            if (page.getList() != null && !"".equals(page.getList())) {
                if (page.getList().size()!=0){
                    return Integer.toString(1);
                }
            }
        }
        return Integer.toString(-1);
    }

}
